package com.ztz.mynewdemo.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.loopj.android.image.SmartImageView;

import java.util.ArrayList;

/**
 * Created by wqewqe on 2017/12/19.
 */

public class BannerAdapter extends PagerAdapter {
    ArrayList<String> cariousImgs;
    Context mContext;

    public BannerAdapter(ArrayList<String> cariousImgs, Context mContext) {
        this.cariousImgs = cariousImgs;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return cariousImgs.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        SmartImageView smartImageView=new SmartImageView(mContext);
        smartImageView.setImageUrl(cariousImgs.get(position));
        smartImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        smartImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cariousShow.showView(position);
            }
        });
        container.addView(smartImageView);
        return smartImageView;
    }
    public CariousShow cariousShow;
    public interface CariousShow
    {
        void showView(int position);
    }

    public void setCariousShow(CariousShow cariousShow) {
        this.cariousShow = cariousShow;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //将object强转成view，然后从viewGroup中清除
        container.removeView((View)object);
    }
    //如果 Item 的位置如果没有发生变化，则返回 POSITION_UNCHANGED。如果返回了 POSITION_NONE，表示该位置的 Item 已经不存在了。
    // 默认的实现是假设 Item 的位置永远不会发生变化，而返回 POSITION_UNCHANGED。
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
